// pages/queren_zhuanghuo/queren_zhuanghuo.js
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
   type:'',
   id:'',
   orderDetail:[],
   isBo:false,
   reason:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

   var id = options.id;
   var type = options.type;
   this.setData({
     id:id,
     type:type
   })

  //  var that = this;
  //   var param = {
  //     oid: id
  //   };
  //   wx.showLoading();
  //   util.commonRequest('orderDetail', 'post', param, function (res) {
  //     wx.hideLoading();
  //     var data = res.data;
  //     if (data.code == 200) {
  //       that.setData({
  //         orderDetail: data.data
  //       })

  //     } else {
  //       wx.showToast({
  //         icon: 'loading',
  //         title: data.msg,
  //       })
  //     }
  //   });

    //this.querenzhuanghuoFun();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  lookLbs: function (e) {
    var src = e.currentTarget.dataset.src;
    wx.previewImage({
      urls: [src],
    })
  },
  lookxiehuo: function (e) {
    var src = e.currentTarget.dataset.src;
    wx.previewImage({
      urls: [src],
    })
  },
  //确认
  confirm: function () {
    var id = this.data.id;
    var type = this.data.type;
    var that = this;
    if (type == 1)//装货确认
    {
      wx.showModal({
        title: '温馨提示',
        content: '请仔细查看订单信息,确认是否“装货”?',
        success(res) {
          if (res.confirm) {
            that.querenzhuanghuoFun(id);
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    } else{
      wx.showModal({
        title: '温馨提示',
        content: '请仔细查看订单信息,确认是否“卸货”?',
        success(res) {
          if (res.confirm) {
            that.querenxiehuoFun(id);
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    }
  },
  //驳回
  back:function(){
    
    this.setData({
      isBo:true
    })
    // this.backFun(id,type);
  },
  cancelFun:function(){
    this.setData({
      isBo: false
    })
  },
  submitFun:function(){
    this.backFun();
  },
  inputText:function(e){
    var text = e.detail.value;
    this.setData({
      reason:text
    })
  },
  backFun: function () {

    var id = this.data.id;
    var type = this.data.type;
    if (!this.data.reason){
      wx.showToast({
        icon:'loading',
        title: '驳回原因不能为空',
      })
      return;
    }
    var that = this;
    var param = {
      id: id,
      type:type,
      reason:that.data.reason
    }
    util.commonRequest('rejectOrder', 'post', param, function (res) {
      var data = res.data;
      wx.hideLoading();
      if (data.code == 200) {
        wx.showToast({
          icon: 'loading',
          title: data.msg,
        })
        if (type == 1){
          wx.redirectTo({
            url: '../shipper_waybill/waybill?type=5',
          })
        }else{
          wx.redirectTo({
            url: '../shipper_waybill/waybill?type=6',
          })
        }
       
      } else {
        wx.showToast({
          icon: 'loading',
          title: data.msg,
        })
      }
    });

  },
  //确认装货
  querenzhuanghuoFun: function (id) {
    var that = this;
    var param = {
      id: id
    }
 
    util.commonRequest('querenzhuanghuo', 'post', param, function (res) {
      var data = res.data;
      wx.hideLoading();
      if (data.code == 200) {
        wx.showToast({
          icon: 'loading',
          title: data.msg,
        })
        wx.redirectTo({
          url: '../shipper_waybill/waybill?type=5',
        })
      } else {
        wx.showToast({
          icon: 'loading',
          title: data.msg,
        })
      }
    });
  },
  querenxiehuoFun: function (id) {
    var that = this;
    var param = {
      id: id
    }
    util.commonRequest('querenxiehuo', 'post', param, function (res) {
      var data = res.data;
      wx.hideLoading();
      if (data.code == 200) {
        wx.showToast({
          icon: 'loading',
          title: data.msg,
        })
        wx.redirectTo({
          url: '../shipper_waybill/waybill?type=-1',
        })
      } else {
        wx.showToast({
          icon: 'loading',
          title: data.msg,
        })
      }
    });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    var param = {
      oid: that.data.id
    };
    wx.showLoading();
    util.commonRequest('orderDetail', 'post', param, function (res) {
      wx.hideLoading();
      var data = res.data;
      if (data.code == 200) {
        that.setData({
          orderDetail: data.data
        })

      } else {
        wx.showToast({
          icon: 'loading',
          title: data.msg,
        })
      }
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})